﻿Imports Microsoft.Win32 'Assists in Registry Reading & Writing

Public Class frmUninstall

    Private strUninstallStrings() As String 'Array to hold Uninstall commands for each program

    Private NewUninstallStrArr 'Filtered array containing ONLY valid uninstall commands

    Private Sub frmUninstall_Load(sender As Object, e As EventArgs) Handles Me.Load

        'Location in Registry where all uninstall "settings" are stored
        Dim ParentKey As RegistryKey = _
            Registry.LocalMachine.OpenSubKey("SOFTWARE\MICROSOFT\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products")

        Dim count As Integer = 0 'Loop Counter

        Dim ChildKey As RegistryKey 'Sub key of Parent key, to read necessary Uninstall properties

        'Loop through each GUID listed
        For Each child As String In ParentKey.GetSubKeyNames()

            ChildKey = ParentKey.OpenSubKey(child).OpenSubKey("InstallProperties") 'Read InstallProperties value(s)

            If Not ChildKey Is Nothing Then 'If not empty, display inside ListView

                Dim LItem As New ListViewItem() 'Create new ListView item

                LItem.Text = ChildKey.GetValue("DisplayName").ToString 'First Column ( Main Item ) Text - Display Name
                LItem.SubItems.Add(ChildKey.GetValue("Publisher").ToString) 'Publisher
                LItem.SubItems.Add(ChildKey.GetValue("InstallDate").ToString) 'Install Date
                LItem.SubItems.Add(ChildKey.GetValue("EstimatedSize").ToString) 'Estimated Size
                LItem.SubItems.Add(ChildKey.GetValue("DisplayVersion").ToString) 'Display Version

                ReDim Preserve strUninstallStrings(count) 'Redim Array

                If ChildKey.GetValue("UninstallString") IsNot Nothing Then 'Determine Uninstall Command(s)

                    strUninstallStrings(count) = ChildKey.GetValue("UninstallString") 'Store each command for each program

                    lvApps.Items.Add(LItem) 'Add ListItem

                Else 'If No Uninstall Command Present, Identify it

                    strUninstallStrings(count) = "No Uninstall String"

                End If

            End If

            count += 1 'Increment Counter for each item

        Next

        'Use LINQ to filter strUninstallStrings array, to only get valid programs with valid uninstall strings
        NewUninstallStrArr = (From str In strUninstallStrings
              Where Not {"No Uninstall String"}.Contains(str)).ToArray() 'New array to be used

    End Sub

    Private Sub lvApps_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvApps.SelectedIndexChanged

        btnUninstall.Visible = True 'Upon itemn selection make these controls visible

        lblPublisher.Visible = True
        lblSize.Visible = True
        lblVersion.Visible = True

        'Display each Installed program's properties
        lblPublisher.Text = lvApps.FocusedItem.SubItems(1).Text
        lblSize.Text = "Size: " & lvApps.FocusedItem.SubItems(3).Text
        lblVersion.Text = "Product Version: " & lvApps.FocusedItem.SubItems(4).Text

    End Sub

    Private Sub btnUninstall_Click(sender As Object, e As EventArgs) Handles btnUninstall.Click

        'Use Shell to execute uninstall command
        Dim procID As Integer

        procID = Shell(NewUninstallStrArr(lvApps.FocusedItem.Index), AppWinStyle.NormalFocus)

    End Sub

End Class
